package com.zhang.family_doctor.config;

import com.zhang.family_doctor.utils.TokenUtils;
import org.json.JSONObject;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * token 验证
 */
@Component
public class TokenInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        //跨域请求会首先发一个option请求，直接返回正常状态并通过拦截器
        if(request.getMethod().equals("OPTIONS")){
            response.setStatus(HttpServletResponse.SC_OK);
            return true;
        }
        response.setCharacterEncoding("utf-8");
        String tokenDoctor = request.getHeader("tokenDoctor");
        String tokenResident = request.getHeader("tokenResident");
        String tokenBackstage = request.getHeader("tokenBackstage");
        if (tokenDoctor!=null){
            boolean result= TokenUtils.verifyDoctor(tokenDoctor);
            if (result){
                System.out.println("通过拦截器");
                return true;
            }
            response.setContentType("application/json; charset=utf-8");
            try {
                JSONObject json=new JSONObject();
                json.put("msg","token verify fail");
                json.put("code","500");
                response.getWriter().append(json.toString());
                System.out.println("认证失败，未通过拦截器");
            } catch (Exception e) {
                return false;
            }
        }else if (tokenResident!=null){
            boolean result= TokenUtils.verifyResident(tokenResident);
            if (result){
                System.out.println("通过拦截器");
                return true;
            }
            response.setContentType("application/json; charset=utf-8");
            try {
                JSONObject json=new JSONObject();
                json.put("msg","tokenResident verify fail");
                json.put("code","500");
                response.getWriter().append(json.toString());
                System.out.println("认证失败，未通过拦截器");
            } catch (Exception e) {
                return false;
            }
        }else if (tokenBackstage!=null){
            boolean result= TokenUtils.verifyBackstage(tokenBackstage);
            if (result){
                System.out.println("通过拦截器");
                return true;
            }
            response.setContentType("application/json; charset=utf-8");
            try {
                JSONObject json=new JSONObject();
                json.put("msg","tokenBackstage verify fail");
                json.put("code","500");
                response.getWriter().append(json.toString());
                System.out.println("认证失败，未通过拦截器");
            } catch (Exception e) {
                return false;
            }
        }else {
            JSONObject json=new JSONObject();
            json.put("msg","tokenResident verify fail");
            json.put("code","500");
            response.getWriter().append(json.toString());
            System.out.println("认证失败，未通过拦截器");
        }

        /**
         * 还可以在此处检验用户存不存在等操作
         */
        return false;
    }
}
